package com.eliza.base.views.database.data

import android.util.Log
import androidx.compose.foundation.layout.Column
import androidx.compose.material3.Button
import androidx.compose.material3.IconButton
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.ui.tooling.preview.Preview
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch

@Preview
@Composable
private fun F() {
    FlowScreen(rememberNavController())
}

@Composable
fun FlowScreen(nav: NavController) {
    lateinit var flow: Flow<Int>
    Column {
        Button(onClick = { flow = setupFlow() }) {
            Text(text = "Start Flow")
        }
        TextButton(onClick = {
            CoroutineScope(Dispatchers.Main).launch {
                flow.collect {
                    Log.d("TAG", it.toString())
                }
            }
        }) {
            Text(text = "Start SetupClicks")
        }
    }


}

fun setupFlow(): Flow<Int> {
    return flow {
        Log.d("", "Start flow")
        (0..10).forEach {
            // Emit items with 500 milliseconds delay
            delay(500)
            Log.d("TAG", "Emitting $it")
            emit(it)

        }
    }.map {
        it + 1
    }.flowOn(Dispatchers.Default)
}
